package com.hwy.widget

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.hwy.widget.drag_list.DragSortActivity
import com.hwy.widget.float_window.FloatWindowActivity
import com.hwy.widget.material.MaterialActivity
import com.hwy.widget.segment.SegmentActivity
import com.hwy.widget.status_bar.StatusBarActivity
import com.hwy.widget.suck_top.SuckTopActivity
import com.hwy.widget.xx_ui.KeUIActivity

class MainActivity : AppCompatActivity() {

    private val dataItems = arrayOf(
        arrayOf("RecyclerView分组悬浮显示", SuckTopActivity::class.java),
        arrayOf("RecyclerView拖拽排序", DragSortActivity::class.java),
        arrayOf("Material组件", MaterialActivity::class.java),
        arrayOf("悬浮窗功能", FloatWindowActivity::class.java),
        arrayOf("状态栏工具类", StatusBarActivity::class.java),
        arrayOf("Segment", SegmentActivity::class.java),
        arrayOf("Ke_UI", KeUIActivity::class.java)
    )

    private var recyclerView: RecyclerView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        initRecycler()

    }

    private fun initRecycler() {

        recyclerView = findViewById<RecyclerView>(R.id.recycler_view)?.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = object : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
                override fun onCreateViewHolder(
                    parent: ViewGroup,
                    viewType: Int
                ): RecyclerView.ViewHolder {
                    return ItemViewHolder(
                        LayoutInflater.from(context)
                            .inflate(R.layout.layout_main_adapter, parent, false)
                    )
                }

                override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
                    (holder as? ItemViewHolder)?.bindView(position)
                }

                override fun getItemCount(): Int {
                    return dataItems.size
                }

            }
        }

    }

    private inner class ItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        fun bindView(position: Int) {
            itemView.findViewById<TextView>(R.id.tv_item)?.run {
                text = dataItems[position][0] as String
                setOnClickListener {
                    startActivity(Intent(this@MainActivity, dataItems[position][1] as Class<*>))
                }
            }
        }
    }

}